<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
      <el-form-item label="标题" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入标题"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:wish:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:wish:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:wish:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:wish:export']"
        >导出</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="wishList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="标题" align="center" prop="title" />

<!--      <el-table-column label="访问人数" align="center" prop="visitNum" />-->
<!--      <el-table-column label="总人数" align="center" prop="totalPeople" />-->
<!--      <el-table-column label="活动地址" align="center" prop="activityPlace" />-->
      <el-table-column label="封面" align="center" prop="cover" >
        <template slot-scope="scope">
          <img :src="scope.row.cover" alt="" width="100px" height="100px">
        </template>
      </el-table-column>
      <el-table-column label="上架状态" align="center">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.shelfStatus"
            active-value="1"
            inactive-value="0"
            @change="handleStatusChange(scope.row)"
          ></el-switch>
        </template>
      </el-table-column>
      <!--<el-table-column label="报名开始时间" align="center" prop="applyStartTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.applyStartTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="报名结束时间" align="center" prop="applyEndTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.applyEndTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="开始时间" align="center" prop="startTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.startTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="结束时间" align="center" prop="endTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.endTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>-->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:wish:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:wish:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改心愿 活动对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="标题" prop="title">
          <el-input v-model="form.title" placeholder="请输入标题" />
        </el-form-item>
        <!--<el-form-item label="1-wish 2-party">
          <el-select v-model="form.type" placeholder="请选择1-wish 2-party">
            <el-option label="请选择字典生成" value="" />
          </el-select>
        </el-form-item>-->
        <el-form-item label="上架状态">
          <el-radio-group v-model="form.shelfStatus">
            <el-radio
              v-for="dict in statusOptions"
              :key="dict.dictValue"
              :label="dict.dictValue"
            >{{dict.dictLabel}}
            </el-radio>
          </el-radio-group>
        </el-form-item>
        <!--<el-form-item label="栏目状态">
          <el-radio-group v-model="form.showStatus">
            <el-radio
              v-for="dict in statusOptions"
              :key="dict.dictValue"
              :label="dict.dictValue"
            >{{dict.dictLabel}}
            </el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="置顶状态">
          <el-radio-group v-model="form.topStatus">
            <el-radio
              v-for="dict in statusTopOptions"
              :key="dict.dictValue"
              :label="dict.dictValue"
            >{{dict.dictLabel}}
            </el-radio>
          </el-radio-group>
        </el-form-item>-->

        <el-form-item hidden="true" label="礼物接收地址" prop="activityPlace">
          <el-input placeholder="请输入礼物接收地址" v-model="form.activityPlace" />
        </el-form-item>
        <el-form-item label="活动人数" prop="totalPeople" hidden="true">
          <el-input value="1" placeholder="请输入活动人数" aria-hidden="true"/>
        </el-form-item>
        <el-form-item label="封面" prop="cover">
          <el-upload
            class="avatar-uploader"
            :action="action"
            :show-file-list="false"
            :on-success="uploadSuccess"
          >
            <img v-if="form.cover" :src="form.cover" class="avatar" />
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
        <!--<el-form-item label="报名开始" prop="applyStartTime">
          <el-date-picker clearable size="small" style="width: 200px"
            v-model="form.applyStartTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择报名开始时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="报名结束" prop="applyEndTime">
          <el-date-picker clearable size="small" style="width: 200px"
            v-model="form.applyEndTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择报名结束时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="活动开始" prop="startTime">
          <el-date-picker clearable size="small" style="width: 200px"
            v-model="form.startTime"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="选择开始时间">
          </el-date-picker>
        </el-form-item>-->
        <el-form-item label="认领结束" prop="endTime">
          <el-date-picker clearable size="small" style="width: 200px"
            v-model="form.endTime"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="选择结束时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="描述" prop="description">
          <Editor v-model="form.description"/>
        </el-form-item>
      </el-form>
      <div class="dialog-footer" slot="footer" style="padding-top:20px">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listWish, getWish, delWish, addWish, updateWish, exportWish } from "@/api/system/wish";
import Editor from '@/components/Editor'
export default {
  name: "Wish",
  components: {
    Editor
  },
  data() {
    return {
      action: process.env.VUE_APP_BASE_API + '/system/upload/file',
      statusOptions: [],
      statusTopOptions: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 心愿 活动表格数据
      wishList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        title: undefined,
        type:1,
        shelfStatus: undefined,
        showStatus: undefined,
        topStatus: undefined,
        visitNum: undefined,
        totalPeople: undefined,
        activityPlace: undefined,
        cover: undefined,
        applyStartTime: undefined,
        applyEndTime: undefined,
        startTime: undefined,
        endTime: undefined,
        description: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        title: [
          { required: true, message: "标题不能为空", trigger: "blur" }
        ],
        type: [
          { required: true, message: "1-wish 2-party不能为空", trigger: "blur" }
        ],
        topStatus: [
          { required: true, message: "置顶状态", trigger: "blur" }
        ],
        cover: [
          { required: true, message: "封面不能为空", trigger: "blur" }
        ],
        endTime: [
          { required: true, message: "结束时间不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getDicts('sys_shelf_status').then(response => {
      this.statusOptions = response.data
    })
    this.getDicts('sys_top_status').then(response => {
      this.statusTopOptions = response.data
    })
  },
  methods: {
    handleStatusChange(row) {
      console.log('aqie', row.shelfStatus)
      let text = row.shelfStatus == '0' ? '下架' : '上架'
      this.$confirm('确认要"' + text + '"活动吗?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        let data = {
          'id': row.id,
          'shelfStatus': row.shelfStatus
        }
        return updateWish(data)
      }).then(() => {
        this.msgSuccess(text + '成功')
      }).catch(function() {
        row.status = row.status == '0' ? '1' : '0'
      })
    },
    uploadSuccess(e) {
      console.log(e)
      if (e.code === 200) {
        this.form.cover = e.data.path
      }
    },
    /** 查询心愿 活动列表 */
    getList() {
      this.loading = true;
      listWish(this.queryParams).then(response => {
        this.wishList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        title: undefined,
        type: undefined,
        shelfStatus: "0",
        showStatus: "0",
        topStatus: "0",
        visitNum: undefined,
        totalPeople: undefined,
        activityPlace: undefined,
        cover: undefined,
        applyStartTime: undefined,
        applyEndTime: undefined,
        startTime: undefined,
        endTime: undefined,
        description: undefined,
        createTime: undefined,
        updateTime: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加心愿 活动";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getWish(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改心愿 活动";
      });
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        console.log('表单提交数据', this.form)
        // return
        if (valid) {
          if (this.form.id != undefined) {
            updateWish(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              }
            });
          } else {
            this.form.totalPeople = 1
            addWish(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除心愿 活动编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delWish(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有心愿 活动数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportWish(queryParams);
        }).then(response => {
          this.download(response.msg);
        }).catch(function() {});
    }
  }
};
</script>

<style>
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }

  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }

  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }

  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
</style>
